<?php
  namespace api\task\controller;
  use think\Controller;
  use think\Db;
  ini_set('memory_limit','3072M');// 临时设置最大内存占用为3G
   set_time_limit(0);// 设置脚本最大执行时间 为0 永不过期
class ExecltaskController {
      public function importExecl(){
       die;
         $fileconnet=file_get_contents('/mnt/www/rili/rilixiaochengxu/public/execldata.txt');
         $res=json_decode($fileconnet);
         try{
               Db::startTrans();
             if($res){
               $dataInstall=[];
                foreach($res as $v){
		//$updata=['jkh'=>$v[7],'zjbxq'=>$v[8],'bx_kssj'=>time(),'update_time'=>time()];
                  // Db::table('cmf_rl_hise_service_project')->where('azd_id',$v[2])->update($updata);
		   $proData=  Db::table('cmf_rl_hise_service_project')->where('azd_id',$v[2])->find();
                  if($proData['gczt']==5){
		    	 $setData=[
                                 'bx_kssj'=>$proData['wgsj'],
				 'zjbxq'=>$v[8],
				'zybjbxq'=>$v[8],
				'update_time'=>time()
				];
                        //  Db::table('cmf_rl_hise_service_project')->where('azd_id',$v[2])->update($setData);
		   }else{
                        $dataInstall[]=$v[2];
                         $unsetData=[
                                 'bx_kssj'=>null,
                                 'zjbxq'=>$v[8],
                                'zybjbxq'=>$v[8],
                                'update_time'=>time()
				];
                          //  Db::table('cmf_rl_hise_service_project')->where('azd_id',$v[2])->update($unsetData);
			}
                 }
              }
 		Db::commit();
           } catch (\Exception $e) {
             Db::rollback();
             echo $e->getMessage();
           } 
                 dump($dataInstall);
      }
         public function zqsync(){
        $page=$_GET['page'];
        $start=3000*($page-1);
        $limit=3000;
//dump($start);
        $res=Db::table('cmf_rl_hise_service_project')->where([
            'is_apply_delay'=>1,
            'gczt'=>['in','3,4']
            ])
->limit($start,$limit)
->select()->toArray();
 //   dump(Db::table('cmf_rl_hise_service_project')->getLastsql()); 
        if($res){
            foreach($res as $resk=>$resv){
  	dump($resv['azd_id']);
                   $sumza= Db::table('cmf_rl_apply_delay')->where(['azd_id'=>$resv['azd_id'],'status'=>1])->sum('yqsqzq');
                   dump($sumza);
                   Db::table('cmf_rl_hise_service_project')->where(['azd_id'=>$resv['azd_id']])->setField([
'yqsqzq'=>$sumza,
'update_time'=>time()
]);
            }
        }
    }
        //运行时间和提交时间补充
	public function tjtime(){
        $data=[];
        
        $res= Db::table('cmf_rl_hise_service_project')->where(['gczt'=>5,'zdyxbs'=>1,'tjrq'=>null])->select()->toArray();
     dump(count($res));die;
         $i=0;
        foreach($res as $k=>$v){
            //调试时间
            if(!$v['tssj']){
                $data['tssj']=$v['wgsj'];
            }
            if(!$v['tjrq']){
                $data['tjrq']=$v['wgsj'];
            }
            if(!empty($data)){
             $i++;
                $data['update_time']=time();
               Db::table('cmf_rl_hise_service_project')->where('azd_id',$v['azd_id'])->update($data);
            }
        
        }
dump($i);
    }

	 // execl导入
    public function provinceChange(){
       require_once "/mnt/www/rili/rilixiaochengxu/api/common/phpexecl/PHPExcel.php";
        $objPHPExcel =new \PHPExcel();
            $file_name = "/mnt/www/rili/province/file.xlsx";//上传文件的地址
            $objReader =\PHPExcel_IOFactory::createReader("Excel2007");
            $obj_PHPExcel =$objReader->load($file_name, $encode = 'utf-8');  //加载文件内容,编码utf-8
            $excel_array=$obj_PHPExcel->getSheet(0)->toArray();   //转换为数组格式
            array_shift($excel_array);  //删除第一个数组(标题);
            //$page=$_GET['page'];
           // $start=$page*100;
           // $end=($page+1)*100;

           // $s=$page*20000;
           // $e=($page+1)*20000;
		//$where['create_time'] = ['between','11111,1111122'];
            foreach($excel_array as $k=>$v) {
             //   if($k<$start ||$k>$end){
              //      continue;
               // }
                //省份
                 //Db::table('cmf_rl_hise_area')->where(['sf_code'=>$v[3]])->setField(['sf_code'=>$v[4],'sf_name'=>$v[2]]);
                
                // if(Db::table('cmf_rl_hise_service_project')->where(['provice'=>$v[3]])->find()){
                  //          Db::table('cmf_rl_hise_service_project')->where(['provice'=>$v[3]])->setField(['provice'=>$v[4]]);
                 //}
                // if(Db::table('cmf_rl_hise_service_project')->where(['city'=>$v[6]])->find()){
                  //          Db::table('cmf_rl_hise_service_project')->where(['city'=>$v[6]])->setField(['city'=>$v[7]]);
                // }
                  if(Db::table('cmf_rl_hise_service_project')->where(['qx'=>$v[9]])->find()){
                            Db::table('cmf_rl_hise_service_project')->where(['qx'=>$v[9]])->setField(['qx'=>$v[10]]);
                 }

                //市
/*		if(Db::table('cmf_rl_hise_dq_table')->where(['dq_code'=>$v[7]])->find()){
    					continue;
			}
                if(!Db::table('cmf_rl_hise_dq_table')->where(['dq_code'=>$v[6]])->find()){
                     $data['sf_code']=$v[4];
		      $data['dq_code']=$v[7];
                     $data['dq_name']=$v[5];
                     $data['active_flag']=1;
                     $data['create_time']=time();
                     $data['update_time']=time();
                     Db::table('cmf_rl_hise_dq_table')->insert($data);
                 }
                 Db::table('cmf_rl_hise_dq_table')->where(['dq_code'=>$v[6]])->setField(['dq_code'=>$v[7],'sf_code'=>$v[4],'dq_name'=>$v[5]]);
                 Db::table('cmf_rl_hise_service_project')->where(['city'=>$v[6]])->setField(['city'=>$v[7]]);*/
                //县
                /*   if(Db::table('cmf_rl_hise_qx_table')->where(['qx_code'=>$v[10]])->find()){
                                        continue;
                        }

                if(!Db::table('cmf_rl_hise_qx_table')->where(['qx_code'=>$v[9]])->find()){
                    $data['qx_code']=$v[10];
                    $data['qx_name']=$v[8];
                    $data['sf_code']=$v[4];
                    $data['dq_code']=$v[7];
                    $data['active_flag']=1;
                    $data['create_time']=time();
			$data['update_time']=time();
                    Db::table('cmf_rl_hise_qx_table')->insert($data);
                }
                Db::table('cmf_rl_hise_qx_table')->where(['qx_code'=>$v[9]])->setField(['qx_code'=>$v[10],'sf_code'=>$v[4],'dq_code'=>$v[7],'qx_name'=>$v[8]]);
                Db::table('cmf_rl_hise_service_project')->where(['qx'=>$v[9]])->setField(['qx'=>$v[10]]);
                  */    
            }  
    }
    public function change(){
		$res=Db::table('cmf_rl_hise_dq_table')->select();
                 foreach($res as $k=>$v){
				$all=Db::table('cmf_rl_hise_dq_table')->where(['dq_code'=>$v['dq_code']])->select();
				if(count($all)>1){
					for($i=0;$i<count($all);$i++){
						if($i!=0){
								Db::table('cmf_rl_hise_dq_table')->where(['dq_code'=>$v['dq_code']])->delete();
							}
					}	
				}
			}
	}
          public function prichange(){
     require_once "/mnt/www/rili/rilixiaochengxu/api/common/phpexecl/PHPExcel.php";
        $objPHPExcel =new \PHPExcel();
            $file_name = "/mnt/www/rili/province/file.xlsx";//上传文件的地址
            $objReader =\PHPExcel_IOFactory::createReader("Excel2007");
            $obj_PHPExcel =$objReader->load($file_name, $encode = 'utf-8');  //加载文件内容,编码utf-8
            $excel_array=$obj_PHPExcel->getSheet(0)->toArray();   //转换为数组格式
            array_shift($excel_array);  //删除第一个数组(标题);





	$data = array_reverse($excel_array);
        $newData = [];
        foreach($data as $ks=>$vs){
            if(!isset($newdata[$vs[6]])){
                $newData[$vs[6]] = $vs;        //里面的id就是你想要根据哪个重复字段的数据内容进行去重
            }
        }
        $excel_array = array_values($newData);
//	dump($excel_array);die;

            $page=$_GET['page'];
            $start=$page*10;
            $end=($page+1)*10;
            foreach($excel_array as $k=>$v) {
                if($k<$start ||$k>$end){
                    continue;
                }



                if(!Db::table('cmf_rl_hise_dq_table')->where(['dq_code'=>$v[6]])->find()){
                     $data['sf_code']=$v[4];
                      $data['dq_code']=$v[7];
                     $data['dq_name']=$v[5];
                     $data['active_flag']=1;
                     $data['create_time']=time();
                     $data['update_time']=time();
                     Db::table('cmf_rl_hise_dq_table')->insert($data);
                 }
                 Db::table('cmf_rl_hise_dq_table')->where(['dq_code'=>$v[6]])->setField(['dq_code'=>$v[7],'sf_code'=>$v[4],'dq_name'=>$v[5]]);
                // Db::table('cmf_rl_hise_service_project')->where(['city'=>$v[6]])->setField(['city'=>$v[7]]);
                //Db::query("update cmf_rl_hise_service_project  set city=".$v[6]." where city=".$v[7]);
           }
         }
}
